# -*- coding: utf-8 -*-

import ast

from ir.ir_helper import Helper, ClassHelper
from ir.ir_ds import ITypeEnum

from .convert_exp import convert_exp


def convert_enum(ast_enum: ast.ClassDef, helper: Helper):
    # Create the enumeration type.
    ir_enum = ITypeEnum(ast_enum.name)
    # Loop for all the body.
    for ast_line in ast_enum.body:
        # Only focus on assignment enumerate.
        if not isinstance(ast_line, ast.Assign):
            continue
        # Get the target of the value.
        ast_target = ast_line.targets[0]
        if not isinstance(ast_target, ast.Name):
            raise Exception('Not implemented Enum assign to non-name type.')
        # Add to enum types.
        ir_enum.consts[ast_target.id] = convert_exp(ast_line.value, helper, None)
    # Create and add the helper.
    enum_helper = ClassHelper()
    enum_helper.base = 'enum'
    enum_helper.member = ir_enum.consts
    # Give back the ir enum.
    return [ir_enum], [enum_helper]